package com.cndw.kungfu.model.activity.holiday;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;

import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.CrossServer;
import com.cndw.kungfu.model.pvp.api.ApiCore;
import com.cndw.kungfu.model.pvp.api.FlowerApiClient;

public class MMFlowerCross {

	private static Set<Integer> sucIds = new HashSet<Integer>();
	private static ConcurrentHashMap<Long, MMFlowerPlayer> mmPlayers = new ConcurrentHashMap<Long, MMFlowerPlayer>();

	private static int state = 0;// 0 正常 1 有服务器没抓到,2 结束

	public static synchronized void runBoss() {

		if (!ApiCore.isCrossServer()) {
			return;
		}
		int initialDelay = 600 - ((TimeProcesser.getUnixTime() - TimeProcesser.getMorningTime()) % 600);
		Context.getTimerServer().scheduleAtFixedRate(new Runnable() {

			@Override
			public void run() {
				try {
					core();
					LogUtil.getMain().info("mmflower cross doing ...");
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}, initialDelay, 600);
		LogUtil.getMain().info("mmflower task delay ..." + initialDelay);
	}

	private static synchronized void core() {
		refresh();

		clacEnd();

	}

	private static synchronized void refresh() {
		sucIds.clear();
		state = 0;
		gotData();

		ArrayList<MMFlowerPlayer> topsTmp = new ArrayList<MMFlowerPlayer>();

		MMFlowerPlayerComparator comparator = new MMFlowerPlayerComparator();
		TreeSet<MMFlowerPlayer> treeSet = new TreeSet<MMFlowerPlayer>(comparator);
		treeSet.addAll(mmPlayers.values());
		for (int i = 0; i < 30; i++) {
			MMFlowerPlayer mmp = treeSet.pollFirst();
			if (mmp != null) {
				topsTmp.add(mmp);
			}
		}
		MMFlowerData.getInstance().setTops(topsTmp);
	}

	private static transient Map<Long, Boolean> awardMap = new HashMap<Long, Boolean>();

	private static synchronized void clacEnd() {
		if (state == 0) {
			if (MMFlowerData.getInstance().isOver()) {
				return;
			}
			if (TimeProcesser.getUnixTime() < MMFlowerData.getInstance().getEndTime()) {
				return;
			}
			int orderId = 0;
			for (MMFlowerPlayer e : MMFlowerData.getInstance().getTops()) {
				orderId++;
				if (!awardMap.containsKey(e.getPid())) {
					try {
						FlowerApiClient.sendAward(orderId, e.getPid(), e.getSid());
						awardMap.put(e.getPid(), true);
						LogUtil.getMain().debug("MMFlower week award send orderId:" + orderId + ",sid:" + e.getSid() + ",pid:" + e.getPid());
					} catch (Exception e2) {
						e2.printStackTrace();
					}
					if (orderId >= 10) {
						break;
					}

				}
			}
			state = 2;
			MMFlowerData.getInstance().over();
			LogUtil.getMain().debug("mm flower week award ...");
		}
	}

	private static synchronized void gotData() {
		boolean add = false;
		for (CrossServer e : CrossServer.getAll()) {
			if (e.getType() <= 0) {
				continue;
			}
			if (sucIds.contains(e.getServerId())) {
				continue;
			}
			add = false;
			try {
				Collection<MMFlowerPlayer> onePlayers = FlowerApiClient.mmflowerPlayer(e.getServerId());
				if (onePlayers != null) {
					for (MMFlowerPlayer mmp : onePlayers) {
						mmPlayers.put(mmp.getPid(), mmp);
					}
					add = true;

				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			if (add) {
				sucIds.add(e.getServerId());
			} else {
				state = 1;
			}
		}
	}

}
